﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClrCalPython;

namespace WpfCalPython
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ClrCalPython.CalPythonFunc _myModel ; 
        public MainWindow()
        {
            InitializeComponent();
            this.ContentRendered += MainWindow_ContentRendered;
        }

        private void MainWindow_ContentRendered(object sender, EventArgs e)
        {
            try
            {
                _myModel = new ClrCalPython.CalPythonFunc();
            }
            catch(Exception ex)
            {
                MessageBox.Show(string.Format("CalPythonFunc 异常 {0}", ex.Message));
            }
            try
            {
               int res= _myModel._init();
                if(res!=0)
                {
                    txtrsult.Text = _myModel.lastMsg;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            double[] x =(from v in  txtX.Text.Trim(new char[] { '[',']',' '}).Split(',')
                        select Convert.ToDouble(v.Trim())).ToArray();
            double[] y = (from v in txtY.Text.Trim(new char[] { '[', ']', ' ' }).Split(',')
                          select Convert.ToDouble(v.Trim())).ToArray();

            int deg =Convert.ToInt32(txtDeg.Text.Trim());
            double[] res=new  double[deg+1];
            _myModel._cal_polyfit(x,y,deg,chkIsShow.IsChecked==true?1:0,ref res);       

            txtrsult.Text =string.Join("\r\n",res) ;
        }

     
    }
}
